package com.bulemobi.activity.ac_nearby;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.*;
import com.baidu.mapapi.model.LatLng;
import com.bulemobi.adapter.BaiduListAdapter;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;

import java.util.ArrayList;

/**
 * Created by 武 on 2014/9/18 0018.
 * 周边服务
 */
public class Ac_NearByYouSelf extends BaseActivity implements View.OnClickListener {
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    private MapView mapView = null;
    private OverlayOptions options;
    private BaiduMap baiduMap;
    private LocationClient mLocationClient = null;
    private BDLocationListener myListener = new MyLocationListener();
    private double nowLatitude;
    private double nowLongitude;
    private boolean isFirstLoc = true;
    //侧滑listview
    @ViewInject(id = R.id.lv_baidu)
    ListView lv_baidu;
    //侧滑ArrayList数据
    private ArrayList<String> nameList;
    //adapter
    private BaiduListAdapter adapter;

    @ViewInject(id = R.id.sildiing)
    LinearLayout sildiing;

    //判断侧滑弹出的状态
    private boolean isFirstDown = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_nearby_main);
        initBaiduMap();
        findActionBar();
        initListData();
    }

    private void initListData() {
        nameList = new ArrayList<String>();
        nameList.add(getResources().getString(R.string.ac_baidu_choice));
        nameList.add(getResources().getString(R.string.ac_baidu_carPark));
        nameList.add(getResources().getString(R.string.ac_baidu_gasStation));
        nameList.add(getResources().getString(R.string.ac_baidu_police));
        nameList.add(getResources().getString(R.string.ac_baidu_pay));
//        nameList.add(getResources().getString(R.string.ac_baidu_camera));
        adapter = new BaiduListAdapter(this, nameList);
        lv_baidu.setAdapter(adapter);
        lv_baidu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
    }

    private void findActionBar() {
        actionBarView.title.setText(getResources().getString(R.string.ac_nearby));
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setBackgroundResource(R.drawable.ac_nearby_filter);
        actionBarView.button_right_icon2.setOnClickListener(this);
        sildiing.setVisibility(View.GONE);
    }

    private void initBaiduMap() {
        mapView = (MapView) findViewById(R.id.map);
        mLocationClient = new LocationClient(this);     //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        baiduMap = mapView.getMap();
        baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //开启交通图
//        baiduMap.setTrafficEnabled(true);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setAddrType("all");
        mLocationClient.setLocOption(option);
        mLocationClient.start();
        requestLocation();
    }

    private void requestLocation() {
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.requestLocation();
        } else {
            Log.d("LocSDK4", "locClient is null or not started");
        }
    }


    @Override
    protected void onDestroy() {
        mLocationClient.stop();
        // 关闭定位图层
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
        mLocationClient.unRegisterLocationListener(myListener);
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    //    点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.actionbar_right_button_icon2:
                if (isFirstDown) {
                    Log.d("OnClick","+++++++++++++");
                    sildiing.setVisibility(View.VISIBLE);
                    TranslateAnimation anim = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 1.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f);
//                    anim.setFillAfter(true);
                    anim.setDuration(2000);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            actionBarView.button_right_icon2.setEnabled(false);
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            actionBarView.button_right_icon2.setEnabled(true);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    sildiing.startAnimation(anim);
                    isFirstDown = false;
                } else {
                    Log.d("OnClick","_________________");
                    sildiing.setVisibility(View.GONE);
                    TranslateAnimation anim = new TranslateAnimation(
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 1.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f,
                            Animation.RELATIVE_TO_SELF, 0.0f);
//                    anim.setFillAfter(true);
                    anim.setDuration(2000);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            actionBarView.button_right_icon2.setEnabled(false);
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            actionBarView.button_right_icon2.setEnabled(true);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    sildiing.startAnimation(anim);
                    isFirstDown = true;
                }
                break;
        }
    }

    class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(final BDLocation location) {
            if (location == null) {
                return;
            }
            closeProgressDialog();
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            baiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.zb);
                options = new MarkerOptions().position(ll).icon(bitmap);
                baiduMap.addOverlay(options);
                baiduMap.animateMapStatus(u);
            }
            BaiduMap.OnMarkerClickListener listener = new BaiduMap.OnMarkerClickListener() {
                /**
                 * 地图 Marker 覆盖物点击事件监听函数
                 * @param marker 被点击的 marker
                 */
                public boolean onMarkerClick(Marker marker) {
                    LatLng latLng = marker.getPosition();
                    return false;
                }
            };
            baiduMap.setOnMarkerClickListener(listener);
        }

        @Override
        public void onReceivePoi(BDLocation bdLocation) {

        }
    }
}
